﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


using Jungle.Code.Utils;
using Jungle.Code.Core;
using Jungle.Users;

namespace Jungle.Users.WebControls
{
    public class TopMenu : WebControl, IPostBackEventHandler
    {
        SessionKeeper sk = new SessionKeeper();
        //------------------------------------------------------------------------------------------------------
        public void RaisePostBackEvent(string eventArgument)
        {
            int pos = eventArgument.IndexOf(',');
            int l = eventArgument.Length;
            string s1 = eventArgument.Substring(0, pos);
            string s2 = eventArgument.Substring(pos + 1, (l - (pos + 1)));

            if (s1 == "ex") // удаление пользователя
            {
                sk.DeleteSession(Context.Session.SessionID);

                HttpCookie cookie = Context.Response.Cookies["RM3ffd3d41ccca0b1b9b2a8923b0b48b3c"];
                cookie["rm"] = "false";

            }
        }
        //--------------------------------------------------------------------
        protected override void RenderContents(HtmlTextWriter writer)
        {
            sk.SessionId = Context.Session.SessionID;
            sk.GetData();

            if (sk.UserId < 1)
                RenderMenu(writer);
            else
            {
                //writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "#003399");
                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                writer.Write("Hello, " + sk.UserLogin + " /");

                ClientScriptManager cs = Page.ClientScript;
                PostBackOptions opt = new PostBackOptions(this);
                opt.ActionUrl = Context.Request.RawUrl;
                opt.Argument = "ex," + sk.UserId.ToString();

                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, cs.GetPostBackEventReference(opt));
                writer.AddAttribute("title", "Exit");
                writer.AddAttribute("style", "cursor: pointer;");
                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                writer.Write(" Exit");
                writer.RenderEndTag();

                writer.RenderEndTag();
            }
            base.RenderContents(writer);
        }
        // -------------------------------------------------------------------
        private HtmlTextWriter RenderMenu(HtmlTextWriter writer)
        {
            //writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "#003399");
            writer.AddAttribute(HtmlTextWriterAttribute.Href, UrlHelper.GetSiteUrl() + "/ru/login.aspx");
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write("login");
            writer.RenderEndTag();

            //writer.Write("&nbsp;/&nbsp;");

            //writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "#003399");
            //writer.AddAttribute(HtmlTextWriterAttribute.Href, UrlHelper.GetSiteUrl() + "/ru/register.aspx");
            //writer.RenderBeginTag(HtmlTextWriterTag.A);
            //writer.Write("registration");
            //writer.RenderEndTag();

            return writer;
        }
    }
}
